package com.feihong.txgw.tx_model;

import android.text.TextUtils;

import com.feihong.txgw.inter.contract.home.IHomeContract;
import com.feihong.txgw.json.HomeJSON;
import com.feihong.txgw.message.RefreshMsgIconMessage;
import com.feihong.txgw.model.bean.home.HomeDialogShowModel;
import com.feihong.txgw.model.bean.user.CityInfo;
import com.feihong.txgw.utils.UserLoginInfo;
import com.feihong.txgw.utils.helper.RxHelper;
import com.feihong.txgw.utils.manage.RetrofitManager;

import java.util.HashMap;

import io.reactivex.Observable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName HomeMainModel
 * @package com.feihong.txgw.tx_model
 * @data 2018/5/3 11:36
 * @Describe
 * @Change
 */
public class HomeMainModel implements IHomeContract.IHomeMainModel {

    public static HomeMainModel create(){
        return new HomeMainModel();
    }

    @Override
    public Observable getHomeAllDate(){
        return Observable.concat(getHomeDataObservable(),getHomeRecommendMsgObservable(),getHomeUnreadMsgObservable()).compose(RxHelper.rxSchedulerHelper());
    }

    @Override
    public Observable<HomeJSON> getHomeData() {
        return getHomeDataObservable().compose(RxHelper.<HomeJSON>rxSchedulerHelper());
    }

    @Override
    public Observable<HomeDialogShowModel> getHomeRecommendMsg() {
        return getHomeRecommendMsgObservable().compose(RxHelper.<HomeDialogShowModel>rxSchedulerHelper());
    }

    @Override
    public Observable<RefreshMsgIconMessage> getHomeUnreadMsg() {
        return getHomeUnreadMsgObservable().compose(RxHelper.<RefreshMsgIconMessage>rxSchedulerHelper());
    }

    private Observable<HomeJSON> getHomeDataObservable(){
        CityInfo cityInfo = UserLoginInfo.getUserCityInfo();
        String cityCode = null;
        if (cityInfo != null && !TextUtils.isEmpty(cityInfo.getCityCode())) {
            cityCode = cityInfo.getCityCode();
        }
        return RetrofitManager.getInstance().getHomeData(cityCode);
    }

    private Observable<HomeDialogShowModel> getHomeRecommendMsgObservable(){
        return RetrofitManager.getInstance().getHomeRecommendMsg(new HashMap<String, String>());
    }

    private Observable<RefreshMsgIconMessage> getHomeUnreadMsgObservable(){
        return RetrofitManager.getInstance().getHomeUnReadMsgCount(new HashMap<String, String>());
    }
}
